package com.facefun.service.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.RowBounds;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.facefun.Enum.StateCode;
import com.facefun.co.CartoonCo;
import com.facefun.dao.CartoonMapper;
import com.facefun.domain.Cartoon;
import com.facefun.domain.CartoonExample;
import com.facefun.domain.CartoonExample.Criteria;
import com.facefun.service.CartoonService;
import com.facefun.util.FileUploadUtil;
import com.facefun.vo.CartoonSearchVo;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class CartoonServiceImpl extends BaseService<Cartoon> implements CartoonService{

	
	@Override
	public List<Cartoon> getRankList() {
		CartoonExample example=new CartoonExample();
		example.setOrderByClause("score desc");
		return mapper.selectByExampleAndRowBounds(example,new RowBounds(0, 11));
	}


	@Override
	public CartoonCo getCartoonCoByKey(Object key) {
		return  ((CartoonMapper) mapper).getCartoonCoByKey(key);
	}

	@Override
	public List<Cartoon> getSearchList(CartoonSearchVo vo) {
		CartoonExample example=new CartoonExample();
		Criteria c= example.createCriteria();
		if(vo.getName()!=null){
			c.andNameLike("%"+vo.getName()+"%");
		
		}
		if(vo.getTc()!=null){
			c.andTpIdEqualTo(vo.getTc());
		}
		if(vo.getJd()!=null){
			c.andStatuEqualTo(vo.getJd());
		}
		return mapper.selectByExample(example);
	}

	@Override
	public Map<String,Object> getCartoonList(CartoonSearchVo vo,Integer offset,Integer limit) {
		Page<Cartoon> p=PageHelper.offsetPage(offset, limit).doSelectPage(new ISelect() {
			
			@Override
			public void doSelect() {
				((CartoonMapper) mapper).getCartoonList(vo);
				
			}
		});
		
		Long recordsTotal=p.getTotal();
		List<Cartoon> data=p.getResult();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("recordsTotal", recordsTotal);
		map.put("recordsFiltered", recordsTotal);
		map.put("data", data);
		return map;
	}


	@Override
	public int addCartoon(MultipartFile Img, MultipartFile Pic, Cartoon cartoon, HttpServletRequest request) {
		String img=Img.isEmpty()?null:FileUploadUtil.upload(Img,request);
		String pic=Pic.isEmpty()?null:FileUploadUtil.upload(Pic,request);
		cartoon.setImg(img);
		cartoon.setPic(pic);
		cartoon.setImg(img);
		cartoon.setPic(pic);
		
		return save(cartoon);
	}


	@Override
	public int updateCartoon(MultipartFile Img, MultipartFile Pic, Cartoon cartoon, HttpServletRequest request) {
		String img=Img.isEmpty()?null:FileUploadUtil.uploadUpdate(Img,cartoon.getImg(), request);
		String pic=Pic.isEmpty()?null:FileUploadUtil.uploadUpdate(Pic,cartoon.getPic(), request);
		cartoon.setImg(img);
		cartoon.setPic(pic);
		
		return updateNotNull(cartoon);
	}


	@Override
	public StateCode removeCartoon(Cartoon cartoon, HttpServletRequest request) {
		int id=cartoon.getId();
		Cartoon c=mapper.selectByPrimaryKey(id);
		FileUploadUtil.uploadDelete(c.getImg(), request);
		FileUploadUtil.uploadDelete(c.getPic(), request);
		
		if(deleteByKey(id)!=0){
			return StateCode.SUCCESS;
		}
			
		return StateCode.NOTEXISIT;
	}

	


	

}
